import urlparse
import Cookie

class WSGIRequest(object):
    def __init__(self, env, start_response):
        self.start_response = start_response
        self.env = env
        self._parse_args()
        self._parse_cookies()

    def _parse_args(self):
        query = self.env['QUERY_STRING']
        if self.env['REQUEST_METHOD'] == 'GET':
            self.GET = urlparse.parse_qs(query, keep_blank_values=True)
            self.REQUEST = self.GET
        elif self.env['REQUEST_METHOD'] == 'POST':
            if query:
                self.POST = urlparse.parse_qs(query, keep_blank_values=True)
            else:
                request_body = self.env['wsgi.input'].read()
                self.POST = urlparse.parse_qs(request_body, keep_blank_values=True)
            self.REQUEST = self.POST
        
    def _parse_cookies(self):
        if 'HTTP_COOKIE' in self.env:
            self.COOKIES = Cookie.SimpleCookie()
            self.COOKIES.load(self.env['HTTP_COOKIE'])
        else:
            self.COOKIES = {}
            
            
